import java.util.Collection;

import org.gruposp2p.openid.provider.*
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.GrantedAuthority;

class BootStrap {

	def springSecurityService

     def init = {  servletContext ->
	   def adminRole = new RoleDNI(authority: 'ROLE_USER').save(flush: true)
	   String password = springSecurityService.encodePassword('password')
	   def username = 'Jose Javier García Zornoza'
	   def	enabled = true
	   def email = 'jgzornoza@gmail.com'
	   def userOpenID = 'http://localhost:8080/openid/user/jj'
	   def nif = '05555555H'
	   def accountNonExpired = true
	   def accountNonLocked = true
	   def credentialsNonExpired = true
	   def grantedAuthorities = new ArrayList<GrantedAuthority>([new GrantedAuthorityImpl('ROLE_ADMIN')]);
       String fullName = 'Jose Javier García'
	   def testUser = new UserDNI(username: nif, password: password, enabled: true, email:email,
    			   userOpenID:userOpenID, nif:nif, accountNonExpired: true,
    			   credentialsNonExpired:true, accountNonLocked:true, id:6)

	   if(testUser.validate()) {
		   testUser.save(flush: true)
		}
		else {
			testUser.errors.allErrors.each {
		        println it
		    }
		}	   
	   UserDNIRoleDNI.create testUser, adminRole, true
	}
     
     def destroy = {
     }
} 
